from django.shortcuts import render,HttpResponse,redirect
from django.core import serializers
from django.db.models import Q
from wordNoteApp import models
from wordNoteApp import urlget
from django.forms.models import model_to_dict
import json
from wordNoteApp import csv_test
import os

# Create your views here.
#127.0.0.1:8000/index?input=1
#127.0.0.1:8000/index?search=1
#127.0.0.1:8000/index?train=1
def index(request):
    search_dict={}
    translateCont=""
    page=request.GET.get("page",None)
    if not page:
        page="1"
    start=(int(page)-1)*10
    end=(int(page)-1)*10+10
    if request.GET.get("type",None)=="input":
        if request.method=="POST":
            wd=request.POST.get("word")
            exp=request.POST.get("explanation")
            rmk=request.POST.get("remark")
            models.wordBook.objects.create(word=wd,explanation=exp,remark=rmk)
    elif request.GET.get("type",None)=="search":
        if request.method=="POST":
            srch=request.POST.get("search")
            search_dict=models.wordBook.objects.filter(Q(word=srch)|Q(explanation=srch))
            #print(search_dict.values())
            #search_dict.update(models.wordBook.objects.filter(explanation=srch).values())
            #print(search_dict)
            return HttpResponse(serializers.serialize("json",search_dict,))
    elif request.GET.get("type",None) == "train":
        if request.method=="POST":
            src=request.POST.get("source",None)
            type=request.POST.get("typenum",None)
            if src:
                translateCont=urlget.getUrl(src,type)
            return HttpResponse(translateCont)
    elif request.GET.get("type", None) == "delete":
        if request.method=="POST":
            id_num = request.POST.get("id")
            models.wordBook.objects.filter(id=id_num).delete()
            respon={"flag":True}
            return HttpResponse(json.dumps(respon))
    elif request.GET.get("type", None) == "deleteall":

        models.wordBook.objects.all().delete()
        respon = {"flag": True}
        return HttpResponse(json.dumps(respon))
    elif request.GET.get("type",None) == "showmark":
        if request.method == "POST":
            id_num = request.POST.get("id")
            remark=models.wordBook.objects.get(id=id_num).remark
            respon = {"flag": True,"remark":remark}
            return HttpResponse(json.dumps(respon))
    elif request.GET.get("type", None) == "remark":
        if request.method == "POST":
            id_num = request.POST.get("id")
            remark =request.POST.get("remark")
            print(remark)
            models.wordBook.objects.filter(id=id_num).update(remark=remark)
    elif request.GET.get("type", None) == "download":
        if request.method == "POST":
            filename=request.POST.get("filename",None)
            query_objs=models.wordBook.objects.all()
            table_dict=[]
            for query_obj in query_objs:
                table_dict.append(model_to_dict(query_obj))
            csv_test.csv_change(table_dict,filename)
    elif request.GET.get("type", None) == "upload":
        if request.method == "POST":
            file=request.FILES.get("csvfile",None)

            f = open(os.path.join('static/downloads',file.name), "wb")
            for chunk in file.chunks():
                f.write(chunk)
            f.close()
            csv_test.csv_update(file.name)
    rowCount=models.wordBook.objects.all().count()
    pagecount,mode=divmod(rowCount,10)
    if mode:
        pagecount+=1
    pagecountlist=[page for page in range(1,pagecount+1)]
    word_dict=models.wordBook.objects.all()[start:end].values()
    return render(request,"index.html",{"allrow":word_dict,"pagecount":pagecountlist})

